import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { IonicPage, NavController, ViewController, NavParams } from 'ionic-angular';

import { GlobalData } from '../../providers/GlobalData';
import { LoginPage } from '../login/login';

/**
 * Generated class for the SettingsGeneralPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-settings-general',
  templateUrl: 'settings-general.html',
})
export class SettingsGeneralPage {

  constructor(public navCtrl: NavController,
    public viewCtrl: ViewController,
    private global: GlobalData,
    private storage: Storage,
    public navParams: NavParams) {

  }

  selectedEnvir(type) {
    this.global.curEnvir = type;
    console.log('selected envir:', type);

    this.storage.get('settings').then(setting => {
      console.log('before set sandbox:', setting.sandbox_mode);

      if (this.global.curEnvir == 'SANDBOX_MODE') {
        setting.sandbox_mode = true;
      } else {
        setting.sandbox_mode = false;
      }
      console.log('after set sandbox:', setting.sandbox_mode);

      this.storage.set('settings', setting);
    });

    this.storage.set('user', null);
    this.navCtrl.setRoot(LoginPage);

  }

  slectedMode(type) {
    this.global.curMode = type;

    this.storage.get('settings').then(setting => {

      if (this.global.curMode == 'ONLINE_MODE') {
        setting.online_mode = true;
      } else {
        setting.online_mode = false;
      }

      this.storage.set('settings', setting);
    });
  }

  nViewDidLoad() {
    console.log('ionViewDidLoad SettingsGeneralPage');
  }

  // dismiss table list modal
  dismiss() {
    this.viewCtrl.dismiss();
  }

}
